﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using jnxc.DBUtility;//Please add references
namespace jnxc.DAL
{
	/// <summary>
	/// 数据访问类:nurseryRecord
	/// </summary>
	public partial class nurseryRecord
	{
		public nurseryRecord()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("nurseryId", "nurseryRecord"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int nurseryId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from nurseryRecord");
			strSql.Append(" where nurseryId=@nurseryId");
			SqlParameter[] parameters = {
					new SqlParameter("@nurseryId", SqlDbType.Int,4)
			};
			parameters[0].Value = nurseryId;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(jnxc.Model.nurseryRecord model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into nurseryRecord(");
			strSql.Append("mhid,mpid,harmParts,incomingArea,incomingWay,findTime,nurseryBH,sampleArea,totalArea,damageLevel,damageArea,isDisaster,disasterArea,investigationName,investigationTime,isProvineOwner,tcxlid,tcdid,aid,uid)");
			strSql.Append(" values (");
			strSql.Append("@mhid,@mpid,@harmParts,@incomingArea,@incomingWay,@findTime,@nurseryBH,@sampleArea,@totalArea,@damageLevel,@damageArea,@isDisaster,@disasterArea,@investigationName,@investigationTime,@isProvineOwner,@tcxlid,@tcdid,@aid,@uid)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@mhid", SqlDbType.Int,4),
					new SqlParameter("@mpid", SqlDbType.Int,4),
					new SqlParameter("@harmParts", SqlDbType.NVarChar,50),
					new SqlParameter("@incomingArea", SqlDbType.NVarChar,50),
					new SqlParameter("@incomingWay", SqlDbType.NVarChar,50),
					new SqlParameter("@findTime", SqlDbType.DateTime),
					new SqlParameter("@nurseryBH", SqlDbType.VarChar,20),
					new SqlParameter("@sampleArea", SqlDbType.Decimal,5),
					new SqlParameter("@totalArea", SqlDbType.Decimal,5),
					new SqlParameter("@damageLevel", SqlDbType.NVarChar,20),
					new SqlParameter("@damageArea", SqlDbType.Decimal,5),
					new SqlParameter("@isDisaster", SqlDbType.Bit,1),
					new SqlParameter("@disasterArea", SqlDbType.Decimal,5),
					new SqlParameter("@investigationName", SqlDbType.NVarChar,20),
					new SqlParameter("@investigationTime", SqlDbType.DateTime),
					new SqlParameter("@isProvineOwner", SqlDbType.Bit,1),
					new SqlParameter("@tcxlid", SqlDbType.Int,4),
					new SqlParameter("@tcdid", SqlDbType.Int,4),
					new SqlParameter("@aid", SqlDbType.Int,4),
					new SqlParameter("@uid", SqlDbType.Int,4)};
			parameters[0].Value = model.mhid;
			parameters[1].Value = model.mpid;
			parameters[2].Value = model.harmParts;
			parameters[3].Value = model.incomingArea;
			parameters[4].Value = model.incomingWay;
			parameters[5].Value = model.findTime;
			parameters[6].Value = model.nurseryBH;
			parameters[7].Value = model.sampleArea;
			parameters[8].Value = model.totalArea;
			parameters[9].Value = model.damageLevel;
			parameters[10].Value = model.damageArea;
			parameters[11].Value = model.isDisaster;
			parameters[12].Value = model.disasterArea;
			parameters[13].Value = model.investigationName;
			parameters[14].Value = model.investigationTime;
			parameters[15].Value = model.isProvineOwner;
			parameters[16].Value = model.tcxlid;
			parameters[17].Value = model.tcdid;
			parameters[18].Value = model.aid;
			parameters[19].Value = model.uid;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(jnxc.Model.nurseryRecord model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update nurseryRecord set ");
			strSql.Append("mhid=@mhid,");
			strSql.Append("mpid=@mpid,");
			strSql.Append("harmParts=@harmParts,");
			strSql.Append("incomingArea=@incomingArea,");
			strSql.Append("incomingWay=@incomingWay,");
			strSql.Append("findTime=@findTime,");
			strSql.Append("nurseryBH=@nurseryBH,");
			strSql.Append("sampleArea=@sampleArea,");
			strSql.Append("totalArea=@totalArea,");
			strSql.Append("damageLevel=@damageLevel,");
			strSql.Append("damageArea=@damageArea,");
			strSql.Append("isDisaster=@isDisaster,");
			strSql.Append("disasterArea=@disasterArea,");
			strSql.Append("investigationName=@investigationName,");
			strSql.Append("investigationTime=@investigationTime,");
			strSql.Append("isProvineOwner=@isProvineOwner,");
			strSql.Append("tcxlid=@tcxlid,");
			strSql.Append("tcdid=@tcdid,");
			strSql.Append("aid=@aid,");
			strSql.Append("uid=@uid");
			strSql.Append(" where nurseryId=@nurseryId");
			SqlParameter[] parameters = {
					new SqlParameter("@mhid", SqlDbType.Int,4),
					new SqlParameter("@mpid", SqlDbType.Int,4),
					new SqlParameter("@harmParts", SqlDbType.NVarChar,50),
					new SqlParameter("@incomingArea", SqlDbType.NVarChar,50),
					new SqlParameter("@incomingWay", SqlDbType.NVarChar,50),
					new SqlParameter("@findTime", SqlDbType.DateTime),
					new SqlParameter("@nurseryBH", SqlDbType.VarChar,20),
					new SqlParameter("@sampleArea", SqlDbType.Decimal,5),
					new SqlParameter("@totalArea", SqlDbType.Decimal,5),
					new SqlParameter("@damageLevel", SqlDbType.NVarChar,20),
					new SqlParameter("@damageArea", SqlDbType.Decimal,5),
					new SqlParameter("@isDisaster", SqlDbType.Bit,1),
					new SqlParameter("@disasterArea", SqlDbType.Decimal,5),
					new SqlParameter("@investigationName", SqlDbType.NVarChar,20),
					new SqlParameter("@investigationTime", SqlDbType.DateTime),
					new SqlParameter("@isProvineOwner", SqlDbType.Bit,1),
					new SqlParameter("@tcxlid", SqlDbType.Int,4),
					new SqlParameter("@tcdid", SqlDbType.Int,4),
					new SqlParameter("@aid", SqlDbType.Int,4),
					new SqlParameter("@uid", SqlDbType.Int,4),
					new SqlParameter("@nurseryId", SqlDbType.Int,4)};
			parameters[0].Value = model.mhid;
			parameters[1].Value = model.mpid;
			parameters[2].Value = model.harmParts;
			parameters[3].Value = model.incomingArea;
			parameters[4].Value = model.incomingWay;
			parameters[5].Value = model.findTime;
			parameters[6].Value = model.nurseryBH;
			parameters[7].Value = model.sampleArea;
			parameters[8].Value = model.totalArea;
			parameters[9].Value = model.damageLevel;
			parameters[10].Value = model.damageArea;
			parameters[11].Value = model.isDisaster;
			parameters[12].Value = model.disasterArea;
			parameters[13].Value = model.investigationName;
			parameters[14].Value = model.investigationTime;
			parameters[15].Value = model.isProvineOwner;
			parameters[16].Value = model.tcxlid;
			parameters[17].Value = model.tcdid;
			parameters[18].Value = model.aid;
			parameters[19].Value = model.uid;
			parameters[20].Value = model.nurseryId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int nurseryId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from nurseryRecord ");
			strSql.Append(" where nurseryId=@nurseryId");
			SqlParameter[] parameters = {
					new SqlParameter("@nurseryId", SqlDbType.Int,4)
			};
			parameters[0].Value = nurseryId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string nurseryIdlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from nurseryRecord ");
			strSql.Append(" where nurseryId in ("+nurseryIdlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public jnxc.Model.nurseryRecord GetModel(int nurseryId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 nurseryId,mhid,mpid,harmParts,incomingArea,incomingWay,findTime,nurseryBH,sampleArea,totalArea,damageLevel,damageArea,isDisaster,disasterArea,investigationName,investigationTime,isProvineOwner,tcxlid,tcdid,aid,uid from nurseryRecord ");
			strSql.Append(" where nurseryId=@nurseryId");
			SqlParameter[] parameters = {
					new SqlParameter("@nurseryId", SqlDbType.Int,4)
			};
			parameters[0].Value = nurseryId;

			jnxc.Model.nurseryRecord model=new jnxc.Model.nurseryRecord();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public jnxc.Model.nurseryRecord DataRowToModel(DataRow row)
		{
			jnxc.Model.nurseryRecord model=new jnxc.Model.nurseryRecord();
			if (row != null)
			{
				if(row["nurseryId"]!=null && row["nurseryId"].ToString()!="")
				{
					model.nurseryId=int.Parse(row["nurseryId"].ToString());
				}
				if(row["mhid"]!=null && row["mhid"].ToString()!="")
				{
					model.mhid=int.Parse(row["mhid"].ToString());
				}
				if(row["mpid"]!=null && row["mpid"].ToString()!="")
				{
					model.mpid=int.Parse(row["mpid"].ToString());
				}
				if(row["harmParts"]!=null)
				{
					model.harmParts=row["harmParts"].ToString();
				}
				if(row["incomingArea"]!=null)
				{
					model.incomingArea=row["incomingArea"].ToString();
				}
				if(row["incomingWay"]!=null)
				{
					model.incomingWay=row["incomingWay"].ToString();
				}
				if(row["findTime"]!=null && row["findTime"].ToString()!="")
				{
					model.findTime=DateTime.Parse(row["findTime"].ToString());
				}
				if(row["nurseryBH"]!=null)
				{
					model.nurseryBH=row["nurseryBH"].ToString();
				}
				if(row["sampleArea"]!=null && row["sampleArea"].ToString()!="")
				{
					model.sampleArea=decimal.Parse(row["sampleArea"].ToString());
				}
				if(row["totalArea"]!=null && row["totalArea"].ToString()!="")
				{
					model.totalArea=decimal.Parse(row["totalArea"].ToString());
				}
				if(row["damageLevel"]!=null)
				{
					model.damageLevel=row["damageLevel"].ToString();
				}
				if(row["damageArea"]!=null && row["damageArea"].ToString()!="")
				{
					model.damageArea=decimal.Parse(row["damageArea"].ToString());
				}
				if(row["isDisaster"]!=null && row["isDisaster"].ToString()!="")
				{
					if((row["isDisaster"].ToString()=="1")||(row["isDisaster"].ToString().ToLower()=="true"))
					{
						model.isDisaster=true;
					}
					else
					{
						model.isDisaster=false;
					}
				}
				if(row["disasterArea"]!=null && row["disasterArea"].ToString()!="")
				{
					model.disasterArea=decimal.Parse(row["disasterArea"].ToString());
				}
				if(row["investigationName"]!=null)
				{
					model.investigationName=row["investigationName"].ToString();
				}
				if(row["investigationTime"]!=null && row["investigationTime"].ToString()!="")
				{
					model.investigationTime=DateTime.Parse(row["investigationTime"].ToString());
				}
				if(row["isProvineOwner"]!=null && row["isProvineOwner"].ToString()!="")
				{
					if((row["isProvineOwner"].ToString()=="1")||(row["isProvineOwner"].ToString().ToLower()=="true"))
					{
						model.isProvineOwner=true;
					}
					else
					{
						model.isProvineOwner=false;
					}
				}
				if(row["tcxlid"]!=null && row["tcxlid"].ToString()!="")
				{
					model.tcxlid=int.Parse(row["tcxlid"].ToString());
				}
				if(row["tcdid"]!=null && row["tcdid"].ToString()!="")
				{
					model.tcdid=int.Parse(row["tcdid"].ToString());
				}
				if(row["aid"]!=null && row["aid"].ToString()!="")
				{
					model.aid=int.Parse(row["aid"].ToString());
				}
				if(row["uid"]!=null && row["uid"].ToString()!="")
				{
					model.uid=int.Parse(row["uid"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select nurseryId,mhid,mpid,harmParts,incomingArea,incomingWay,findTime,nurseryBH,sampleArea,totalArea,damageLevel,damageArea,isDisaster,disasterArea,investigationName,investigationTime,isProvineOwner,tcxlid,tcdid,aid,uid ");
			strSql.Append(" FROM nurseryRecord ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" nurseryId,mhid,mpid,harmParts,incomingArea,incomingWay,findTime,nurseryBH,sampleArea,totalArea,damageLevel,damageArea,isDisaster,disasterArea,investigationName,investigationTime,isProvineOwner,tcxlid,tcdid,aid,uid ");
			strSql.Append(" FROM nurseryRecord ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM VnurseryRecord ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.nurseryId desc");
			}
			strSql.Append(")AS Row, T.*  from VnurseryRecord T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "nurseryRecord";
			parameters[1].Value = "nurseryId";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod
        public DataSet GetListByPagedis(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.tcbh desc");
            }
            strSql.Append(")AS Row, T.*  from (SELECT DISTINCT pname, pbh, cname, cbh, coname, cobh, tname, tbh, tcdname,tcdid FROM dbo.VnurseryRecord ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(")  T ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
        public int GetRecordCountdis(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM (SELECT DISTINCT pname, pbh, cname, cbh, coname, cobh, tname, tbh, tcdname FROM dbo.VnurseryRecord ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(")  T");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        public DataSet GetListV(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM VnurseryRecord ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount7(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM (SELECT MTT.mhid,coname,cname,MTT.pname, disarea,dbo.Vmyplant.pname AS plantname, dbo.Vmyharmful.hname, dbo.Vmyharmful.hbm, dbo.Vmyharmful.ldxm,[harmParts],level1,level2,level3 FROM (select mhid, [mpid],[harmParts],coname,cname,pname,sum(disarea) as disarea,MAX( CASE damageLevel WHEN '重度' THEN sumarea ELSE 0 END) level3, max(CASE damageLevel WHEN '中度' THEN sumarea ELSE 0 END) level2,max(CASE  WHEN damageLevel in('轻度','轻度以下') THEN sumarea ELSE 0 END) level1 from (SELECT mhid,[mpid],[harmParts],coname,cname,pname,[damageLevel],sum([damageArea]) as sumarea,sum([disasterArea]) as disarea FROM [Vlypc] inner join Vareapcct on vlypc.aid= Vareapcct.tid");//);
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" group by [mhid],[mpid],[harmParts],coname,cname,pname,[damageLevel]	) as MT group by mhid, [mpid],[harmParts],coname,cname,pname) as MTT INNER JOIN dbo.Vmyharmful ON MTT.mhid = dbo.Vmyharmful.mhid INNER JOIN dbo.Vmyplant ON MTT.mpid = dbo.Vmyplant.mpid) as TTT");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage7(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.mhid desc");
            }
            strSql.Append(")AS Row, T.*  from (SELECT MTT.mhid,coname,cname,MTT.pname, disarea,dbo.Vmyplant.pname AS plantname, dbo.Vmyharmful.hname, dbo.Vmyharmful.hbm, dbo.Vmyharmful.ldxm,[harmParts],level1,level2,level3 FROM (select mhid, [mpid],[harmParts],coname,cname,pname,sum(disarea) as disarea,MAX( CASE damageLevel WHEN '重度' THEN sumarea ELSE 0 END) level3, max(CASE damageLevel WHEN '中度' THEN sumarea ELSE 0 END) level2,max(CASE  WHEN damageLevel in('轻度','轻度以下') THEN sumarea ELSE 0 END) level1 from (SELECT mhid,[mpid],[harmParts],coname,cname,pname,[damageLevel],sum([damageArea]) as sumarea,sum([disasterArea]) as disarea FROM [Vlypc] inner join Vareapcct on vlypc.aid= Vareapcct.tid ");//" T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" group by [mhid],[mpid],[harmParts],coname,cname,pname,[damageLevel]	) as MT group by mhid, [mpid],[harmParts],coname,cname,pname) as MTT INNER JOIN dbo.Vmyharmful ON MTT.mhid = dbo.Vmyharmful.mhid INNER JOIN dbo.Vmyplant ON MTT.mpid = dbo.Vmyplant.mpid) as T");
            
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        public int GetRecordCount9(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM (SELECT MTT.mhid, disarea, coname,cname,MTT.pname,dbo.Vmyplant.pname AS plantname, dbo.Vmyharmful.hname, dbo.Vmyharmful.hbm, dbo.Vmyharmful.ldxm,[harmParts],level1,level2,level3,[incomingArea],[incomingWay],[findtime] FROM(select mhid, [mpid],[harmParts],coname,cname,pname,[incomingArea],[incomingWay],[findtime],sum(disarea) as disarea,MAX( CASE damageLevel WHEN '重度' THEN sumarea ELSE 0 END) level3, max(CASE damageLevel WHEN '中度' THEN sumarea ELSE 0 END) level2,max(CASE  WHEN damageLevel in('轻度','轻度以下') THEN sumarea ELSE 0 END) level1 from ( SELECT mhid,[mpid],[harmParts],coname,cname,pname,[incomingArea],[incomingWay],[findtime],[damageLevel],sum([damageArea]) as sumarea,sum([disasterArea]) as disarea FROM [Vlypcw] inner join Vareapcct on vlypcw.aid= Vareapcct.tid ");//);
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" group by [mhid],[mpid],[harmParts],coname,cname,pname,[incomingArea],[incomingWay],[findtime],[damageLevel]) as MT group by mhid, [mpid],[harmParts],coname,cname,pname,[incomingArea],[incomingWay],[findtime]) as MTT INNER JOIN dbo.Vmyharmful ON MTT.mhid = dbo.Vmyharmful.mhid INNER JOIN dbo.Vmyplant ON MTT.mpid = dbo.Vmyplant.mpid) as TTT");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage9(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.mhid desc");
            }
            strSql.Append(")AS Row, T.*  from (SELECT MTT.mhid, disarea, coname,cname,MTT.pname,dbo.Vmyplant.pname AS plantname, dbo.Vmyharmful.hname, dbo.Vmyharmful.hbm, dbo.Vmyharmful.ldxm,[harmParts],level1,level2,level3,[incomingArea],[incomingWay],[findtime] FROM(select mhid, [mpid],[harmParts],coname,cname,pname,[incomingArea],[incomingWay],[findtime],sum(disarea) as disarea,MAX( CASE damageLevel WHEN '重度' THEN sumarea ELSE 0 END) level3, max(CASE damageLevel WHEN '中度' THEN sumarea ELSE 0 END) level2,max(CASE  WHEN damageLevel in('轻度','轻度以下') THEN sumarea ELSE 0 END) level1 from ( SELECT mhid,[mpid],[harmParts],coname,cname,pname,[incomingArea],[incomingWay],[findtime],[damageLevel],sum([damageArea]) as sumarea,sum([disasterArea]) as disarea FROM [Vlypcw] inner join Vareapcct on vlypcw.aid= Vareapcct.tid");//" T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" group by [mhid],[mpid],[harmParts],coname,cname,pname,[incomingArea],[incomingWay],[findtime],[damageLevel]) as MT group by mhid, [mpid],[harmParts],coname,cname,pname,[incomingArea],[incomingWay],[findtime]) as MTT INNER JOIN dbo.Vmyharmful ON MTT.mhid = dbo.Vmyharmful.mhid INNER JOIN dbo.Vmyplant ON MTT.mpid = dbo.Vmyplant.mpid) as T");

            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
		#endregion  ExtensionMethod
	}
}

